/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aic.beans;

import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
public class Order {

//    private static int cntId = 0;
    private String id;
    private Date orderDate;
    private List<Item> items = new LinkedList<Item>();

    public Order() {
//        cntId++;
//        this.id = "orderNumber" + cntId;
        UUID uuid = UUID.randomUUID();
        this.id = uuid.toString();
        this.orderDate = new Date();
    }

    @Override
    public String toString() {
        String items = "items: ";
        if (this.items.size() > 0) {
            for (Iterator<Item> item = this.items.iterator(); item.hasNext();) {
                items += item.next().getProduct_id() + ", ";
            }
            items = items.substring(0, items.length()-2);
        } else {
            items += "null";
        }
        return "id: " + this.id + " / orderDate: " + this.orderDate + " / " + items;
    }

    @XmlAttribute
    @XmlID
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @XmlElement
    @XmlJavaTypeAdapter(DateAdapter.class)
    public Date getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(Date orderDate) {
        this.orderDate = orderDate;
    }

    @XmlElement(name = "item")
    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Order other = (Order) obj;
        if ((this.id == null) ? (other.id != null) : !this.id.equals(other.id)) {
            return false;
        }
        if (this.orderDate != other.orderDate && (this.orderDate == null || !this.orderDate.equals(other.orderDate))) {
            return false;
        }
        if (this.items != other.items && (this.items == null || !this.items.equals(other.items))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 37 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 37 * hash + (this.orderDate != null ? this.orderDate.hashCode() : 0);
        hash = 37 * hash + (this.items != null ? this.items.hashCode() : 0);
        return hash;
    }

    
}
